//
//  JJRNManager.m
//  reactNativeDemo
//
//  Created by hamilyjing on 2018/12/15.
//  Copyright © 2018 Facebook. All rights reserved.
//

#import "JJRNManager.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>

@implementation JJRNManager

+ (instancetype)sharedInstance {
  static dispatch_once_t once;
  static JJRNManager *instance;
  dispatch_once(&once, ^{
    instance = [[self alloc] init];
  });
  return instance;
}

- (RCTBridge *)bridge {
  if (!_bridge) {
#ifdef  DEBUG
    NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
    _bridge = [[RCTBridge alloc] initWithBundleURL:jsCodeLocation moduleProvider:nil launchOptions:nil];
    return _bridge;
#endif
    
    //NSURL *sourceURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
    //            NSURL *sourceURL = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
    //        NSURL *sourceURL = [NSURL URLWithString:@"http://10.200.90.38:8081/index.ios.bundle?platform=ios&dev=true"];
    
//    NSURL *sourceURL=[[SNBundleHelper shared]getMainBundlePath];
//    _bridge = [[RCTBridge alloc] initWithBundleURL:sourceURL moduleProvider:nil launchOptions:nil];
  }
  return _bridge;
}

- (void)sendEventToRnWithName:(NSString *)eventName body:(id)body {
  [self.bridge enqueueJSCall:@"RCTDeviceEventEmitter"
                      method:@"emit"
                        args:body ? @[eventName, body] : @[eventName]
                  completion:NULL];
}

@end
